package br.com.ccm.session.util;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * UtilitÃ¡rio para HTML.
 * 
 * @author Marlon Silva Carvalho
 * @since 10/05/2010
 */
final public class HTML {

	private HTML() {
		throw new AssertionError();
	}

	/**
	 * Encontrar e retornar todos os links encontrados no texto.
	 * 
	 * @param texto
	 *            Texto.
	 * @return Array de links encontrados.
	 */
	public static String[] acharLinks(String texto) {
		Collection<String> encontrados = new ArrayList<String>();
		String pattern = "https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?";
		Pattern padrao = Pattern.compile(pattern);
		Matcher pesquisa = padrao.matcher(texto);
		while (pesquisa.find()) {
			encontrados.add(pesquisa.group());
		}
		return encontrados.toArray(new String[] {});
	}

	/**
	 * Remover todas as tags de um texto.
	 * 
	 * @param texto
	 *            Texto que terÃ¡ as tags removidas.
	 * @return Texto com as tags removidas.
	 */
	public static String removerTags(String texto) {
		String noHTMLString = texto.replaceAll("\\<.*?\\>", "");
		return noHTMLString;
	}

	/**
	 * Substituir do texto os elementos HTML especiais, como &nbsp;, pelo valor
	 * correspondente em ASCII. CÃ³digo original em
	 * http://www.rgagnon.com/javadetails/java-0307.html.
	 * 
	 * @param source
	 *            CÃ³digo que terÃ¡ o texto trocado.
	 * @param start
	 *            Onde iniciar a troca.
	 * @return Texto com os valores convertidos.
	 */
	public static String desconverterElementosHTMLEspeciais(String source, int start) {
		HashMap<String, String> htmlEntities;
		htmlEntities = new HashMap<String, String>();
		htmlEntities.put("&lt;", "<");
		htmlEntities.put("&gt;", ">");
		htmlEntities.put("&amp;", "&");
		htmlEntities.put("&quot;", "\"");
		htmlEntities.put("&agrave;", "Ã ");
		htmlEntities.put("&Agrave;", "Ã€");
		htmlEntities.put("&atilde;", "Ã£");
		htmlEntities.put("&Atilde;", "Ãƒ");
		htmlEntities.put("&aacute;", "Ã¡");
		htmlEntities.put("&Aacute;", "Ã�");
		htmlEntities.put("&acirc;", "Ã¢");
		htmlEntities.put("&auml;", "Ã¤");
		htmlEntities.put("&Auml;", "Ã„");
		htmlEntities.put("&Acirc;", "Ã‚");
		htmlEntities.put("&aring;", "Ã¥");
		htmlEntities.put("&Aring;", "Ã…");
		htmlEntities.put("&aelig;", "Ã¦");
		htmlEntities.put("&AElig;", "Ã†");
		htmlEntities.put("&ccedil;", "Ã§");
		htmlEntities.put("&Ccedil;", "Ã‡");
		htmlEntities.put("&eacute;", "Ã©");
		htmlEntities.put("&Eacute;", "Ã‰");
		htmlEntities.put("&egrave;", "Ã¨");
		htmlEntities.put("&Egrave;", "Ãˆ");
		htmlEntities.put("&ecirc;", "Ãª");
		htmlEntities.put("&Ecirc;", "ÃŠ");
		htmlEntities.put("&euml;", "Ã«");
		htmlEntities.put("&Euml;", "Ã‹");
		htmlEntities.put("&iuml;", "Ã¯");
		htmlEntities.put("&Iuml;", "Ã�");
		htmlEntities.put("&iacute;", "Ã­");
		htmlEntities.put("&Iacute;", "Ã�");
		htmlEntities.put("&ocirc;", "Ã´");
		htmlEntities.put("&Ocirc;", "Ã”");
		htmlEntities.put("&otilde;", "Ãµ");
		htmlEntities.put("&Otilde;", "Ã•");
		htmlEntities.put("&oacute;", "Ã³");
		htmlEntities.put("&Oacute;", "Ã“");
		htmlEntities.put("&uacute;", "Ãº");
		htmlEntities.put("&Uacute;", "Ãš");
		htmlEntities.put("&ouml;", "Ã¶");
		htmlEntities.put("&Ouml;", "Ã–");
		htmlEntities.put("&oslash;", "Ã¸");
		htmlEntities.put("&Oslash;", "Ã˜");
		htmlEntities.put("&szlig;", "ÃŸ");
		htmlEntities.put("&ugrave;", "Ã¹");
		htmlEntities.put("&Ugrave;", "Ã™");
		htmlEntities.put("&ucirc;", "Ã»");
		htmlEntities.put("&Ucirc;", "Ã›");
		htmlEntities.put("&uuml;", "Ã¼");
		htmlEntities.put("&Uuml;", "Ãœ");
		htmlEntities.put("&nbsp;", " ");
		htmlEntities.put("&copy;", "\u00a9");
		htmlEntities.put("&reg;", "\u00ae");
		htmlEntities.put("&euro;", "\u20a0");
		int i, j;
		i = source.indexOf("&", start);
		if (i > -1) {
			j = source.indexOf(";", i);
			if (j > i) {
				String entityToLookFor = source.substring(i, j + 1);
				String value = (String) htmlEntities.get(entityToLookFor);
				if (value != null) {
					source = new StringBuffer().append(source.substring(0, i))
							.append(value).append(source.substring(j + 1))
							.toString();
				}
				return desconverterElementosHTMLEspeciais(source, i + 1);
			}
		}
		return source;
	}

	/**
	 * Converte caracteres especiais para elementos HTML. CÃ³digo "gentilmente sugado" do site
	 * 
	 * @param source String que terÃ¡ os elementos substituÃ­dos.
	 * @return Texto formatado.
	 */
	public static final String converterParaElementosHTMLEspeciais(String source) {
		HashMap<String, String> htmlEntities;
		htmlEntities = new HashMap<String, String>();
		htmlEntities.put("<", "&lt;");
		htmlEntities.put(">", "&gt;");
		htmlEntities.put("&", "&amp;");
		htmlEntities.put("\\", "&quot;");
		htmlEntities.put("Ã ", "&agrave;");
		htmlEntities.put("Ã€", "&Agrave;");
		htmlEntities.put("Ã£", "&atilde;");
		htmlEntities.put("Ãƒ", "&Atilde;");
		htmlEntities.put("Ã¡", "&aacute;");
		htmlEntities.put("Ã�", "&Aacute;");
		htmlEntities.put("Ã¢", "&acirc;");
		htmlEntities.put("Ã¤", "&auml;");
		htmlEntities.put("Ã„", "&Auml;");
		htmlEntities.put("Ã‚", "&Acirc;");
		htmlEntities.put("Ã¥", "&aring;");
		htmlEntities.put("Ã…", "&Aring;");
		htmlEntities.put("Ã¦", "&aelig;");
		htmlEntities.put("Ã†", "&AElig;");
		htmlEntities.put("Ã§", "&ccedil;");
		htmlEntities.put("Ã‡", "&Ccedil;");
		htmlEntities.put("Ã©", "&eacute;");
		htmlEntities.put("Ã‰", "&Eacute;");
		htmlEntities.put("Ã¨", "&egrave;");
		htmlEntities.put("Ãˆ", "&Egrave;");
		htmlEntities.put("Ãª", "&ecirc;");
		htmlEntities.put("ÃŠ", "&Ecirc;");
		htmlEntities.put("Ã«", "&euml;");
		htmlEntities.put("Ã‹", "&Euml;");
		htmlEntities.put("Ã¯", "&iuml;");
		htmlEntities.put("Ã�", "&Iuml;");
		htmlEntities.put("Ã­", "&iacute;");
		htmlEntities.put("Ã�", "&Iacute;");
		htmlEntities.put("Ã´", "&ocirc;");
		htmlEntities.put("Ã”", "&Ocirc;");
		htmlEntities.put("Ãµ", "&otilde;");
		htmlEntities.put("Ã•", "&Otilde;");
		htmlEntities.put("Ã³", "&oacute;");
		htmlEntities.put("Ã“", "&Oacute;");
		htmlEntities.put("Ãº", "&uacute;");
		htmlEntities.put("Ãš", "&Uacute;");
		htmlEntities.put("Ã¶", "&ouml;");
		htmlEntities.put("Ã–", "&Ouml;");
		htmlEntities.put("Ã¸", "&oslash;");
		htmlEntities.put("Ã˜", "&Oslash;");
		htmlEntities.put("ÃŸ", "&szlig;");
		htmlEntities.put("Ã¹", "&ugrave;");
		htmlEntities.put("Ã™", "&Ugrave;");
		htmlEntities.put("Ã»", "&ucirc;");
		htmlEntities.put("Ã›", "&Ucirc;");
		htmlEntities.put("Ã¼", "&uuml;");
		htmlEntities.put("Ãœ", "&Uuml;");
		htmlEntities.put(" ", "&nbsp;");
		htmlEntities.put("\u00a9", "&copy;");
		htmlEntities.put("\u00ae", "&reg;");
		htmlEntities.put("\u20a0", "&euro;");
		int length = source.length();
		StringBuilder sb = new StringBuilder();
		for(int i=0; i < length; i++) {
			char ch = source.charAt(i);
			if ( htmlEntities.containsKey(ch) ) {
				String o = htmlEntities.get(ch);
				sb.append(o);
			} else {
				sb.append(ch);
			}
		}
		return sb.toString();
	}

}